<?php
namespace Home\Controller;
use Think\Controller;
class SignController extends Controller {


    /**
     * 点击签到
     */
    public function click(){
        $uid = I('post.uid',0,'intval');
        if(!$uid) returnApiError('uid参数错误');
        $sign = M('sign');
        $member = M('member');
        if(!$members=$member->find($uid)){
            returnApiError(1,'会员不存在');
        }
        $time = date("Y-m-d");

        //判断是否签到过
        $where['member_id'] = $uid;
        $where['sign_time'] = $time;
        $data = $sign->where($where)->order(array('sign_id'=>'desc'))->find();
        if($data) returnApiError(1,'已经签到过了');

        //判断是否第一次签到
        if(!$sign->where("member_id = $uid")->find()){
            $num = 1;
        }else{
            //判断是否连续签到
            $yesterday = date("Y-m-d",strtotime("-1 day"));
            $yesterday_where['member_id'] = $uid;
            $yesterday_where['sign_time'] = $yesterday;
            $is_sign = $sign->where($yesterday_where)->order(array('sign_id'=>'desc'))->find();

            if(!$is_sign){
                $num = 1;
            }else{
                $num = $is_sign['sign_num']+1;
            }

        }

        $add['sign_time'] = $time;
        $add['member_id'] = $uid;
        $add['sign_num'] = $num;
        $add['points'] = $is_sign['points']?$is_sign['points']:0;
        $sep = $is_sign['points']? ',' : "";
        //签到可赠送积分
        if($num == 5)$add["points"] .=  $sep.'10';
        if($num == 15)$add["points"] .= $sep.'30';
        if($num == 30)$add["points"] .= $sep.'100';
        if($sign->add($add)){
            //签到送积分
            $pointlog = D('PointsLog'); 
            $insertarr["pl_memberid"] =$members['member_id'];
            $insertarr["pl_membername"] =$members['member_name'];
            $points = $pointlog->insert("signin",$insertarr);
            returnApiSuccess('ok');
        }
        returnApiError(1,'签到失败');
    }

    /**
     * 签到信息
     */
    public function index(){
        $uid = I('post.uid',0,'intval');
        if(!$uid) returnApiError('uid参数错误');
        $time = date('Y-m-d');
        $member = M('member');
        if(!$member->find($uid)){
            returnApiError(1,'会员不存在');
        }

        $sign = M('sign');
        $data = $sign->field('sign_time')->where("member_id=$uid")->select();
        $result = array();
        $result['list'] = array_values($data);
        $state = $sign->where("member_id = $uid and sign_time = '$time'")->order(array('sign_id'=>'desc'))->find();
        $result["is_sign"] = $state ? 1 : 0;
        $sign_num = $sign->field('sign_num')->where("member_id=$uid")->order(array('sign_id'=>'desc'))->find();
        $result['sign_num'] = $sign_num['sign_num'] ? $sign_num['sign_num'] : 0;
        returnApiSuccess('ok',$result);
    }
    //签到兑换积分列表
     public function exchangelist(){
         $uid = I('post.uid',0,'intval');
         $member = M('member');
         $sign = M('sign');
        if(!$member->find($uid)){
            returnApiError(1,'会员不存在');
        }
        //取最后一条
        $lastrow =$sign->field("sign_id,member_id,sign_time,sign_num,points")->where(array("member_id"=>$uid))->order(array("sign_id"=>"desc"))->limit(1)->select();
        if(!$lastrow)returnApiError(1,'数据为空！');
        $row = $lastrow[0];
        $num =  floor($row["sign_num"]/5); 
        $points_list = array();
        if($num<0.1)returnApiError(1,'没有积分赠送！');
        if($row["points"]) $points=explode(",",$row["points"]);
        $points_list =  $this->pointsarr();
        foreach ($points_list as $k =>$v){
            if($num>0){
                 $points_list[$k]["status"] =in_array($v['points'], $points)?0:1;
            }
            if($num>1){
                 $points_list[$k]["status"] =in_array($v['points'], $points)?0:1;
            }
            if($num>4){
                 $points_list[$k]["status"] =in_array($v['points'], $points)?0:1;
            }
        }

        returnApiSuccess('ok',$points_list);
     }
     //兑换积分
     public function exchange(){
         $uid = I('post.uid',0,'intval');
         $point = I('post.points',0,'intval');
         $member = M('member');
         $sign = M('sign');
         $exchangelog = M("signexchange_log");
        if(!$member->find($uid)){
            returnApiError(1,'会员不存在');
        }
        //取最后一条
        $lastrow =$sign->field("sign_id,member_id,sign_time,sign_num,points")->where(array("member_id"=>$uid))->order(array("sign_id"=>"desc"))->limit(1)->select();
        if(!$lastrow)returnApiError(1,'数据为空！');
        $row = $lastrow[0];
        if(!$row["points"])returnApiError(1,'没有积分可以兑换！');
        if($row["points"]) $points=explode(",",$row["points"]);
        $is_empty = TRUE;
        foreach ($points as $k=>$v){
            if($v==$point){
                $member->where(array("member_id"=>$uid))->setInc('member_points', $points[$k]);
                unset($points[$k]);
                $is_empty = false;
            }
        }
        if($is_empty)returnApiError(1,'积分已兑换！');
        $newstr=  implode(",", $points);
        
        if(!$newstr && $row["sign_num"]==30){
            $sdata["member_id"] = $uid;
            $sdata["sign_time"] = date("Y-m-d");
            $sdata["sign_num"] = 0;
            $sdata["points"] =0;
            $sign->add($sdata);
        }
        $return =$sign->where(array("sign_id"=>$row['sign_id']))->save(array("points"=>$newstr));
        if($return){
//            $data["owner_id"] = $uid;
//            $data["points"]   = $point;
//            $data["exchange_status"] = 1;
//            $data["addtime"] = time();
//            $exchangelog->add($data);
            //签到兑换积分
            $users = $member->field("member_name")->where(array("member_id"=>$uid))->find();
            $pointlog = D('PointsLog'); 
            $insertarr["pl_memberid"] = $uid;
            $insertarr["pl_membername"] = $users["member_name"];
            $insertarr['pl_desc'] = "连续签到送积分";
            $insertarr['pl_points'] = $point;
            $points = $pointlog->insert("app",$insertarr);
            returnApiSuccess('ok');
        }
        returnApiSuccess('ok');
     }
     //规则列表
     public function pointsarr(){
         return array(
                 array("day"=>5,"points"=>10),
                 array("day"=>15,"points"=>30),
                 array("day"=>30,"points"=>100),
         );
     }
}

